require 'yaml'
IRCBot::Plugin.define 'badluck' do
	help_string 'Shows how often one has been kicked by any of the bots as a bad-luck kick. Use as "badluck" with no arguments for yourself, or "badluck <nick>" for someone else.'
	@kicks = YAML::load(File.open($config['global']['plugin_data_dir']+'/kicklog.yml'))
	if !@kicks then
		@kicks = {}
	end
	
	def kick_function(kicker,channel,kicked,message)
		if message.strip.downcase == "bad luck; cheese's fault." then
			kicked = kicked.downcase
			channel = channel.sub('#','').downcase
			if !@kicks[kicked] then @kicks[kicked] = {}; end
			if !@kicks[kicked][channel] then @kicks[kicked][channel] = {}; end
			if !@kicks[kicked][channel][kicker] then @kicks[kicked][channel][kicker] = 0; end
			@kicks[kicked][channel][kicker] = @kicks[kicked][channel][kicker].to_i + 1
			File.open($config['global']['plugin_data_dir']+'/kicklog.yml','w') do |file|
				file << @kicks.to_yaml
			end
		end
	end
	
	def command(user,target,message,hostmask)
		if message.empty? then
			who_to_check = user.downcase
			person = user
		else
			who_to_check = message.downcase.strip
			person = message.strip
		end
		if @kicks[who_to_check] then
			output = "#{person}'s stats:"
			for channel in @kicks[who_to_check]
				output << " in ##{channel[0]}, "
				for kicker in channel[1]
					output << "#{kicker[1]} #{(kicker[1] > 1 ? "times" : 'time')} by #{kicker[0].capitalize}, "
				end
				output = output.strip
				output[-1] = ';'
			end
			output[-1] = '.'
			privmsg(target,output)
		else
			privmsg(target,"#{person} hasn't been kicked by any bot... yet.")
		end
	end
end
